<!--
 * @Description:点击空白处隐藏
 * @Author: Sun
 * @Date: 2019-07-01 20:05:50
 * @LastEditors: Sun
 * @LastEditTime: 2019-07-22 21:47:02
 -->
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>点击空白处隐藏</title>
	<style>
		* {
			margin: 0;
			padding: 0;
		}

		body {
			height: 2000px;
		}

		#mask {
			width: 100%;
			height: 100%;
			opacity: .4;
			background-color: black;
			position: absolute;
			top: 0;
			left: 0;
			display: none;
		}

		#show {
			width: 300px;
			height: 300px;
			background-color: #fff;
			position: fixed;
			left: 50%;
			top: 50%;
			margin: -150px 0 0 -150px;
			display: none;
		}
	</style>
	<script>
		window.onload = function () {
			function $(id) {
				return document.getElementById(id);
			}
			var login = $("login");
			login.onclick = function (event) {
				var event = event || window.event;
				$("mask").style.display = "block"
				$("show").style.display = "block"
				document.body.style.overflow = "hidden"
				if (event && event.stopPropagation) {
					event.stopPropagation()
				} else { //ie
					event.cancelBubble = true
				}
			}

			document.onclick = function (event) {
				var event = event || window.event;
				//判断当前对象
				var targetId = event.target ? event.target.id : event.srcElement.id;
				if (targetId != "show") { //不等于当前对象就隐藏
					$("mask").style.display = "none"
					$("show").style.display = "none"
					document.body.style.overflow = "visible"
				}
			}

		}
	</script>
</head>

<body>
	<a href="javascript:;" id="login">注册</a>
	<a href="javascript:;">登陆</a>
	<div id="mask" class="km"></div>
	<div id="show" class="bm"></div>
</body>

</html>